<script setup>
import { ref } from 'vue'
import { Toast } from 'vant'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user.js'
import { getCaptchaImage, sendSmsCaptcha, loginService } from '@/api/user'

const router = useRouter()

const userStore = useUserStore()
const { setUserData } = userStore

const n = ref(0)
const timer = ref(null)
const loading = ref(false)
const captchaImageData = ref(null)
const captchaImageKey = ref(null)
const phone = ref(null)
const code = ref(null)
const sms = ref(null)

const getPhoneCode = async () => {
  if (!phone.value) {
    Toast('请先输入手机号')
    return
  }

  if (!/^1\d{10}$/.test(phone.value)) {
    Toast('请输入正确的手机号格式')
    return
  }

  if (!code.value) {
    Toast('请输入图形验证码')
    return
  }

  const { message } = await sendSmsCaptcha({
    captchaKey: captchaImageKey.value,
    captchaCode: code.value,
    mobile: phone.value
  })
  Toast(message)

  n.value = 60
  timer.value = setInterval(() => {
    if (n.value === 0) {
      clearInterval(timer.value)
      return
    }
    n.value--
  }, 1000)
}

const login = async () => {
  const { message, data } = await loginService({
    smsCode: String(sms.value),
    mobile: String(phone.value),
    isParty: false,
    partyData: {}
  })
  setUserData(data)
  Toast(message)
  setTimeout(() => {
    router.back()
  }, 500)
}

const init = async () => {
  loading.value = false
  const {
    data: { base64, key }
  } = await getCaptchaImage()
  captchaImageData.value = base64
  captchaImageKey.value = key
  loading.value = true
}
init()
</script>

<template>
  <div class="login">
    <!-- 标题 -->
    <van-nav-bar
      title="会员登录"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- 内容 -->
    <div class="content">
      <div class="container">
        <div class="header">
          <p class="title">手机号登录</p>
          <p class="sub-title">未注册的手机号登录后将自动注册</p>
        </div>
        <div class="form">
          <p>
            <input type="number" placeholder="请输入手机号码" v-model="phone" />
          </p>
          <p>
            <input type="text" placeholder="请输入图形验证码" v-model="code" />
            <img :src="captchaImageData" v-show="loading" />
          </p>
          <p>
            <input type="number" placeholder="请输入短信验证码" v-model="sms" />
            <span class="code-btn" v-if="n === 0" @click="getPhoneCode"
              >获取验证码</span
            >
            <span class="resend" v-else>重新发送({{ n }})秒</span>
          </p>
          <button @click="login">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.login {
  padding-top: 46px;
  padding-bottom: 50px;
  background-color: white;
}

.van-nav-bar {
  ::v-deep .van-nav-bar__title {
    font-weight: bold;
  }

  ::v-deep .van-icon-arrow-left {
    color: black;
  }
}

.content {
  width: 100%;
  height: 667px;

  .container {
    width: 315px;
    height: 567px;
    margin: 40px;

    .title {
      color: #191919;
      font-size: 27px;
    }

    .sub-title {
      margin-top: 10px;
      color: #b3b3b3;
      font-size: 14px;
    }
  }

  .form {
    width: 315px;
    height: 257px;
    margin-top: 50px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;

    p {
      width: 297px;
      height: 29px;
      padding: 9px;
      border-bottom: 0.5px solid #f3f1f2;
      display: flex;
      justify-content: space-between;
      align-items: center;

      input {
        border: none;
        padding: 10px 0;
        font-size: 14px;
        color: gray;
      }
    }

    p:nth-child(2) {
      img {
        width: 96px;
        height: 29px;
      }
    }

    .code-btn {
      color: #cea26a !important;
      font-size: 14px !important;
    }

    .resend {
      color: #9e9e9e;
      font-size: 14px;
    }

    button {
      width: 100%;
      height: 43px;
      margin-top: 40px;
      background: linear-gradient(90deg, #ecb53c, #ff9211);
      color: #fff;
      border-radius: 40px;
      box-shadow: 0 10px 20px 0 rgba(0, 0, 0, 0.1);
      letter-spacing: 2px;
      border: none;
    }
  }
}
</style>
